import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/home/Home.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/home"
  },
  {
    path: "/home",
    name: "home",
    component: Home,
    meta: {
      title: "首页"
    }
  },
  {
    path: "/category",
    name: "category",
    component: () => import("../views/category/Category.vue"),
    meta: {
      title: "分类"
    }
  },
  {
    path: "/cart",
    name: "cart",
    component: () => import("../views/cart/Cart.vue"),
    meta: {
      title: "购物车"
    }
  },
  {
    path: "/profile",
    name: "profile",
    component: () => import("../views/profile/Profile.vue"),
    meta: {
      title: "我的"
    }
  },
  {
    path: "/detail/:iid",
    name: "detail",
    component: () => import("../views/detail/Detail.vue"),
    meta: {
      title: "详情"
    }
  }
];

const router = new VueRouter({
  routes
});
// 导航守卫
router.beforeEach((to, from, next) => {
  if (to.matched[0].meta) {
    document.title = to.matched[0].meta.title;
  }
  next();
});

export default router;
